package com.qzxy.premiumhotelsystem.controller.common;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.qzxy.premiumhotelsystem.common.annotation.LogExecutionTime;
import com.qzxy.premiumhotelsystem.common.constant.CaptchaConstant;
import com.qzxy.premiumhotelsystem.common.enums.LogLevel;
import com.qzxy.premiumhotelsystem.domain.pojo.Mail;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.service.MailService;
import com.qzxy.premiumhotelsystem.utils.KeysUtil;
import com.qzxy.premiumhotelsystem.utils.VerificationCodeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: iQNRen
 * @Description: 邮箱接口
 * @Date: 2024/9/18 20:17 周三
 * @Version: 1.0
 */
@RestController
@Tag(name = "通用接口")
@RequestMapping("/mail")
public class MailController {
    @Resource
    private MailService mailService;

    /**
     * 发送注册邮件
     *
     * @param email
     * @return
     */
    @Operation(summary = "发送邮件_注册用户")
    @PostMapping("/simple/{email}")
    public Result SendVerificationCodeMessage(@PathVariable @Valid String email) {
        String verification6Code = VerificationCodeUtils.getVerification6Code();

        Mail mail = new Mail();
        mail.setSubject("臻品酒店");
        mail.setSendTo(email);
        // 存入CaptchaStore设置5分钟之后过期
        String key = CaptchaConstant.REGISTER_V_CODE.concat("_"+email);
        KeysUtil.addCaptcha(key, verification6Code, 5*60);

        mail.setText("【臻品酒店】尊敬的臻品酒店用户，您正在注册，您的你的验证码为: " + verification6Code + "，该验证码5分钟内有效，请勿泄露他人。");
        mailService.sendSimpleMail(mail);
        return Result.ok("验证码已发送至邮箱，请注意查收");
    }

    /**
     * 重置密码
     *
     * @param email
     * @return
     */
    @Operation(summary = "发送邮件_忘记密码", description = "忘记密码发送邮件")
    @PostMapping("/forget-password/{email}")
    public Result SendForgetPasswordCodeMessage(@PathVariable @Valid String email) {
        String verification6Code = VerificationCodeUtils.getVerification6Code();
        Mail mail = new Mail();
        mail.setSubject("臻品酒店");
        mail.setSendTo(email);
        String key = CaptchaConstant.FORGET_V_CODE.concat("_" + email);
        KeysUtil.addCaptcha(key, verification6Code, 5 * 60);
        mail.setText("【臻品酒店】尊敬的臻品酒店用户，您正在重置密码，您的你的验证码为: " + verification6Code + "，该验证码5分钟内有效，请勿泄露他人。");
        mailService.sendSimpleMail(mail);
        return Result.ok("验证码已发送至邮箱，请注意查收");
    }

    /**
     * 更新密码
     *
     * @param email
     * @return
     */
    @SaCheckLogin
    @Operation(summary = "发送邮件_更新密码")
    @LogExecutionTime(message = "发送邮件_更新密码",level = LogLevel.DEBUG)
    @PostMapping("/update-password/{email}")
    public Result SendUpdatePasswordCodeMessage(@PathVariable @Valid String email) {
        String verification6Code = VerificationCodeUtils.getVerification6Code();
        Mail mail = new Mail();
        mail.setSubject("臻品酒店");
        mail.setSendTo(email);
        String key = CaptchaConstant.UPDATE_V_CODE.concat("_" + email);
        KeysUtil.addCaptcha(key, verification6Code, 5 * 60);
        mail.setText("【臻品酒店】尊敬的臻品酒店用户，您正在更新密码，您的你的验证码为: " + verification6Code + "，该验证码5分钟内有效，请勿泄露他人。");
        mailService.sendSimpleMail(mail);
        return Result.ok("验证码已发送至邮箱，请注意查收");
    }
}

